///*
// * Copyright (c) 2019. The copyright is reserved by Ghode of Harbin Institute
// * of Technology. Users are free to copy, change or remove. Because no one
// * will read this. Only I know is that Repeaters are the best of the world.
// * Only I know is that Repeaters are the best of the world. Only I know is
// * that Repeaters are the best of the world. Maybe a long copyright text
// * seems professional. Therefore this text will be a bit lengthy. However,
// * the author seems to be afraid that one day, this text may be uploaded to
// * business projects. That is the time you can contact with author via email
// * ghode@cirnocraft.im or directly ignore this, which will be interesting.
// */
//
//package com.ghode.demospring.database;
//
//import com.ghode.demospring.config.DatabaseShardingAlgorithm;
//import com.ghode.demospring.config.TableShardingAlgorithm;
//import com.dangdang.ddframe.rdb.sharding.api.ShardingDataSourceFactory;
//import com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule;
//import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
//import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule;
//import com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy;
//import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy;
//import com.dangdang.ddframe.rdb.sharding.keygen.DefaultKeyGenerator;
//import com.dangdang.ddframe.rdb.sharding.keygen.KeyGenerator;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//import javax.sql.DataSource;
//import java.sql.SQLException;
//import java.util.Arrays;
//import java.util.HashMap;
//import java.util.Map;
//
///**
// * @author yangyang
// * @date 2019/1/29
// */
//@Configuration
//public class DataSourceConfig {
//
//    @Autowired
//    private Database2Config database2Config;
//
//    @Autowired
//    private Database3Config database3Config;
//
//    @Autowired
//    private DatabaseShardingAlgorithm databaseShardingAlgorithm;
//
//    @Autowired
//    private TableShardingAlgorithm tableShardingAlgorithm;
//
//    @Bean
//    public DataSource getDataSource() throws SQLException {
//        return buildDataSource();
//    }
//
//    private DataSource buildDataSource() throws SQLException {
//        //分库设置
//        Map<String, DataSource> dataSourceMap = new HashMap<>(2);
//        //添加两个数据库database2和database3
//        dataSourceMap.put(database2Config.getDatabaseName(), database2Config.createDataSource());
//        dataSourceMap.put(database3Config.getDatabaseName(), database3Config.createDataSource());
//        //设置默认数据库
//        DataSourceRule dataSourceRule = new DataSourceRule(dataSourceMap, database2Config.getDatabaseName());
//
//        //分表设置，大致思想就是将查询虚拟表Goods根据一定规则映射到真实表中去
//        TableRule orderTableRule = TableRule.builder("goods")
//                .actualTables(Arrays.asList("goods_0", "goods_1"))
//                .dataSourceRule(dataSourceRule)
//                .build();
//
//        //分库分表策略
//        ShardingRule shardingRule = ShardingRule.builder()
//                .dataSourceRule(dataSourceRule)
//                .tableRules(Arrays.asList(orderTableRule))
//                .databaseShardingStrategy(new DatabaseShardingStrategy("goods_id", databaseShardingAlgorithm))
//                .tableShardingStrategy(new TableShardingStrategy("goods_type", tableShardingAlgorithm)).build();
//        DataSource dataSource = ShardingDataSourceFactory.createDataSource(shardingRule);
//        return dataSource;
//    }
//
//
//    @Bean
//    public KeyGenerator keyGenerator() {
//        return new DefaultKeyGenerator();
//    }
//
//}